﻿//
// Created by Grishul Eugeny
//
// See license in License.txt
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using NCommander.NodeProviders;
using NCommander.Core;

namespace NCommander
{
	public partial class PanelView : UserControl
	{
		public PanelView()
		{
			InitializeComponent();
		}

		public void CreateTab( UserControl view )
		{
			var result = new TabPage( ( ( IViewProvider ) view ).TabCaption );

			view.Dock = DockStyle.Fill;
			view.Refresh();

			result.Controls.Add( view );

			_fileTabs.Controls.Add( result );
			Refresh();
		}

		public NodeProvider CurrentProvider
		{
			get
			{
				return ( ( IViewProvider ) _fileTabs.SelectedTab.Controls[0] ).NodeProvider;
			}
		}

		public Node[] SelectedNodes
		{
			get
			{
				return ( ( IViewProvider ) _fileTabs.SelectedTab.Controls[0] ).SelectedNodes;
			}
		}

		public override void Refresh()
		{
			try
			{
				_currentDrive.BeginUpdate();

				_diskViews.Controls.Clear();
				_currentDrive.Items.Clear();

				foreach( var item in DriveInfo.GetDrives() )
				{
					var button = new Button();
					button.Text = item.Name;
					button.Size = new Size( 40, 40 );
					var ii = IconProviders.Win32Shell.Instance.GetIcon( item.Name );
					button.BackgroundImage = ii.ToBitmap();
					button.BackgroundImageLayout = ImageLayout.Center;

					_diskViews.Controls.Add( button );
					_currentDrive.Items.Add( item.Name );
				}

			}
			finally
			{
				_currentDrive.EndUpdate();
			}

			base.Refresh();
		}

		private void _currentDrive_SelectedIndexChanged( object sender, EventArgs e )
		{
			var drive = DriveInfo.GetDrives().FirstOrDefault( x => x.Name.Equals( _currentDrive.SelectedItem ) );

			var format = CultureInfo.CurrentCulture.NumberFormat.Clone() as NumberFormatInfo;
			format.NumberGroupSizes = Enumerable.Range( 1, 10 ).Select( x => 3 ).ToArray();
			format.NumberGroupSeparator = "";

			format.CurrencyGroupSizes = Enumerable.Range( 1, 10 ).Select( x => 3 ).ToArray();
			format.CurrencyGroupSeparator = "";

			_driveSescription.Text = string.Format( format, "[{0}] {1} Кб из {2} Кб свободно", drive.VolumeLabel, drive.AvailableFreeSpace / 1024, drive.TotalFreeSpace / 1024 );
		}

		public void SelectAll()
		{
			( ( IViewProvider ) _fileTabs.SelectedTab.Controls[0] ).SelectAll();
		}
	}
}
